Imports BitSmoke.Core.Data
Imports BitSmoke.Core.Data.Profiles
Imports BitSmoke.Core.Data.Preferences

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Namespace Core.Storage
    Public Class Store
        Public Shared ReadOnly Property RoamingPath() As String
            Get
                Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\BitSmoke\"

                If Not Directory.Exists(sPath) Then
                    Directory.CreateDirectory(sPath)
                End If

                Return sPath
            End Get
        End Property
        Public Shared Sub load(ByRef pDataRoot As DataRoot)
            'preferences
            pDataRoot.Preferences = loadPrefs()

            'my profile
            pDataRoot.MyProfile = loadMyProfile()

            'all other profiles
            loadProfiles(pDataRoot)

            'groups
            'files
        End Sub

        Public Shared Sub save(ByRef pDataRoot As DataRoot)
            'TODO: clear directory first?

            'preferences
            savePrefs(pDataRoot.Preferences)

            'my profile
            saveMyProfile(pDataRoot.MyProfile)

            'all other profiles
            saveProfiles(pDataRoot)

            'groups
            'files
        End Sub

        Private Shared Function loadObject(ByVal pS_FileName As String) As Object
            Dim obj As Object = Nothing

            Dim sFileName As String = RoamingPath & pS_FileName

            If File.Exists(sFileName) Then
                Dim fs As New FileStream(sFileName, FileMode.Open)

                Dim bf As New BinaryFormatter
                obj = bf.Deserialize(fs)

                fs.Close()
            End If

            Return obj
        End Function

        Private Shared Sub saveObject(ByVal pObj As Object, ByVal pS_FileName As String)
            Dim sFileName As String = RoamingPath & pS_FileName

            Dim fs As New FileStream(sFileName, FileMode.Create)

            Dim bf As New BinaryFormatter
            bf.Serialize(fs, pObj)

            fs.Close()
        End Sub

        Private Shared Function loadPrefs() As MyPreferences
            Dim prefs As MyPreferences = CType(loadObject(MyPreferences.FileName), MyPreferences)

            If prefs Is Nothing Then
                'setup defaults
                prefs = New MyPreferences(True)
            End If

            Return prefs
        End Function

        Private Shared Sub savePrefs(ByVal pPrefs As MyPreferences)
            saveObject(pPrefs, MyPreferences.FileName)
        End Sub

        Private Shared Function loadUserProfile(ByVal pS_FileName As String) As UserProfile
            Dim profile As UserProfile = CType(loadObject(pS_FileName), UserProfile)

            If profile Is Nothing Then
                'setup defaults
                profile = New UserProfile(True)
            End If

            Return profile
        End Function

        Private Shared Function loadMyProfile() As MyProfile
            Dim profile As MyProfile = CType(loadObject(MyProfile.FileName), MyProfile)

            If profile Is Nothing Then
                'setup defaults
                profile = New MyProfile(True)
            End If

            Return profile
        End Function

        Private Shared Sub saveMyProfile(ByVal pProfile As MyProfile)
            saveObject(pProfile, MyProfile.FileName)
        End Sub

        Private Shared Sub loadProfiles(ByRef pDataRoot As DataRoot)
            Dim di As New DirectoryInfo(RoamingPath)
            Dim fi As FileInfo
            Dim up As UserProfile

            For Each fi In di.GetFileSystemInfos("*.profile")
                If fi.Name <> MyProfile.FileName Then
                    up = CType(loadObject(fi.Name), UserProfile)

                    pDataRoot.UserProfilesByGUID.Add(up.GUID, up)
                End If
            Next
        End Sub

        Private Shared Sub saveProfiles(ByRef pDataRoot As DataRoot)
            Dim kvp As KeyValuePair(Of Guid, UserProfile)

            For Each kvp In pDataRoot.UserProfilesByGUID
                saveObject(kvp.Value, kvp.Value.GUID.ToString & ".profile")
            Next
        End Sub
    End Class
End Namespace
